package org.jeecg.modules.flowable.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.flowable.engine.repository.ProcessDefinition;
import org.flowable.engine.runtime.ProcessInstance;
import org.flowable.task.api.Task;
import org.flowable.task.api.history.HistoricTaskInstance;
import org.jeecg.common.api.vo.Result;
import org.jeecg.modules.flowable.service.FlowableService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/flowable")
@Tag(name="flowable工作流API")
public class FlowableController {

    @Autowired
    private FlowableService flowableService;

    /** 部署流程 */
    @PostMapping("/deploy")
    @Operation(summary="部署流程")
    public Result<String> deployProcess() {
        try {
            String deploymentId = flowableService.deployProcess();
            return Result.ok("流程部署成功，部署ID: " + deploymentId);
        } catch (Exception e) {
            return Result.error("流程部署失败: " + e.getMessage());
        }
    }

    /** 启动流程 */
    @PostMapping("/start")
    @Operation(summary="启动流程")
    public Result<String> startProcess(
            @RequestParam String businessKey,
            @RequestParam String applicant,
            @RequestParam Integer days,
            @RequestParam String reason) {
        try {
            ProcessInstance processInstance = flowableService.startLeaveProcess(businessKey, applicant, days, reason);
            return Result.ok("流程启动成功，流程实例ID: " + processInstance.getId());
        } catch (Exception e) {
            return Result.error("流程启动失败: " + e.getMessage());
        }
    }

    /** 查询任务 */
    @GetMapping("/tasks")
    @Operation(summary="查询任务")
    public Result<List<Task>> tasks(@RequestParam String assignee) {
        try {
            List<Task> tasks = flowableService.getTasks(assignee);
            return Result.ok(tasks);
        } catch (Exception e) {
            return Result.error("查询任务失败: " + e.getMessage());
        }
    }

    /** 完成任务 */
    @PostMapping("/complete")
    @Operation(summary="完成任务")
    public Result<String> complete(
            @RequestParam String taskId,
            @RequestParam Boolean approved,
            @RequestParam(required = false) String approval) {
        try {
            flowableService.completeTask(taskId, approved, approval);
            return Result.ok("任务完成成功");
        } catch (Exception e) {
            return Result.error("任务完成失败: " + e.getMessage());
        }
    }

    /** 查询流程定义 */
    @GetMapping("/process-definitions")
    @Operation(summary="查询流程定义")
    public Result<List<ProcessDefinition>> getProcessDefinitions() {
        try {
            List<ProcessDefinition> processDefinitions = flowableService.getProcessDefinitions();
            return Result.ok(processDefinitions);
        } catch (Exception e) {
            return Result.error("查询流程定义失败: " + e.getMessage());
        }
    }

    /** 根据ID查询流程实例 */
    @GetMapping("/process-instance")
    @Operation(summary="根据ID查询流程实例")
    public Result<ProcessInstance> getProcessInstance(@RequestParam String processInstanceId) {
        try {
            ProcessInstance processInstance = flowableService.getProcessInstance(processInstanceId);
            if (processInstance != null) {
                return Result.ok(processInstance);
            } else {
                return Result.error("未找到对应的流程实例");
            }
        } catch (Exception e) {
            return Result.error("查询流程实例失败: " + e.getMessage());
        }
    }

    /** 查询历史任务 */
    @GetMapping("/history-tasks")
    @Operation(summary="查询历史任务")
    public Result<List<HistoricTaskInstance>> getHistoryTasks(@RequestParam String processInstanceId) {
        try {
            List<HistoricTaskInstance> historyTasks = flowableService.getHistoryTasks(processInstanceId);
            return Result.ok(historyTasks);
        } catch (Exception e) {
            return Result.error("查询历史任务失败: " + e.getMessage());
        }
    }

    /** 查询流程变量 */
    @GetMapping("/process-variables")
    @Operation(summary="查询流程变量")
    public Result<Map<String, Object>> getProcessVariables(@RequestParam String processInstanceId) {
        try {
            Map<String, Object> processVariables = flowableService.getProcessVariables(processInstanceId);
            return Result.ok(processVariables);
        } catch (Exception e) {
            return Result.error("查询流程变量失败: " + e.getMessage());
        }
    }
}